package com.Example;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.MassData;
import com.badlogic.gdx.utils.Logger;
// TODO mass and gravity needs to be taken care of.

public class BoxPigeon extends BaseBoxObject{

	
	private static final float MAXSPEED = 10f;
	public static float MAX_STAMINA = 2000;
	public static float MAX_HEALTH = 100;
	public static float MAX_SHEILD = 100;
	
	TextureWrapper texture;
	Vector2 pos;
	float stateTime=0;
	private float xSpeed = 5/200f;
	static float yspeed = 1f;
	static float density = 2f;
	static float restitution = 0.5f;
	static float widthPixel = 30f;
	static float heightPixel = 20f; 
	static float radius = 0; 
	
	
	public static float health; // health of pigeon from 0 to 100
	public static int shield; // shield of pigeon from 0 to 3
	
	public static float stamina; // stamina of pigeon from 0 to 100 ,, when it
	// is 0

	
	public BoxPigeon(int bodyIndex, int collisionGroup,	BodyType bodyType,float px,float py,
			float angle) {
		super(bodyIndex, collisionGroup);

		stamina = MAX_STAMINA;
		health= MAX_HEALTH;

		
		pos=new Vector2(px,py);
	
		MakeBody(widthPixel, heightPixel, radius, bodyType, density, restitution, pos, angle);
		body.setGravityScale(.1f);
		TextureRegion keyframe = Assets.pigeonAnimation.getKeyFrame(Gdx.graphics.getDeltaTime(), true);
		texture=new TextureWrapper(keyframe, pos);
		
	}
	
	public void SetTextureDimension(int width,int height){
		texture.SetDimension(width, height);
	}

	@Override
	public void Draw(SpriteBatch sp) {
		// TODO Auto-generated method stub
		if(IsAlive){
			texture.Draw(sp);
		}
	}
	
	public void Update(float dt){
		if(IsAlive){
			super.Update(dt);
			stateTime += dt;
			
			TextureRegion keyframe = Assets.pigeonAnimation.getKeyFrame(stateTime, true);
			
			
			texture.SetTexture(keyframe);
			
			
			float yVelocity = this.getYVelocity();
			
			Vector2 currentVelocity = body.getLinearVelocity();
			currentVelocity.add(new Vector2(xSpeed , yVelocity));
			
			if(currentVelocity.x >= MAXSPEED){
				currentVelocity.add(new Vector2(-xSpeed , 0));
			}
			
			this.body.setLinearVelocity(currentVelocity);
	//		message = "after:" + this.body.getLinearVelocity().x + ","+ this.body.getLinearVelocity().y;

			texture.Position.set(bodyWorldPosition);
		}
		
	}
	
	private float getYVelocity(){
		boolean isTouched  = Gdx.input.isTouched();
		if(isTouched && stamina>0){
	//		stamina=stamina-5; 
			return BoxPigeon.yspeed;
		}else{
			if (stamina < MAX_STAMINA) {
				stamina = stamina + 1;
			}
			return 0;
		}
	}
	
	public float GetBodyRotationInDegrees(){
		return body.getAngle()*MathUtils.radiansToDegrees;
	}

	public static Vector2 getPosition() {
		// TODO Auto-generated method stub
		return bodyWorldPosition;
	}
}
